package meijuli.com.join51.business.ui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.HeaderViewListAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;


/**
 * @author Scott Smith  @Date 2016年04月16/4/22日 14:56
 */
public class LoadMoreListView extends LoadMoreViewBase implements PtrUIHandler {
    private ListView mListView;
    private DefaultFooterView mFooterView;
    private AbsListView.OnScrollListener onScrollListener;
    private boolean mReachBottom = false;
    private PullListView.OnPullListener onPullListener;
    private boolean isLoading = false;
    private boolean canLoadMore = true;
    private boolean isRefreshing = false;

    public LoadMoreListView(Context context, ListView listView) {
        super(context);
        mListView = listView;
        initView(context);
    }

    private void initView(Context context) {
        setLayoutParams(new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        setOrientation(HORIZONTAL);
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(null != onScrollListener) {
                    onScrollListener.onScrollStateChanged(view,scrollState);
                }

                if(SCROLL_STATE_IDLE == scrollState && mReachBottom) {
                    onReachBottom();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if(null != onScrollListener) {
                    onScrollListener.onScroll(view,firstVisibleItem,visibleItemCount,totalItemCount);
                }

                mReachBottom = firstVisibleItem + visibleItemCount >= totalItemCount - 1;
            }
        });
        mListView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        addView(mListView);
    }

    @Override
    public View loadMoreView() {
        return mListView;
    }

    @Override
    public void addFooterView(View view) {
        if(mListView.getFooterViewsCount() <= 0 && mListView.getAdapter() != null) {
            mListView.addFooterView(view);
        }
    }

    @Override
    public void removeFooterView(View view) {
        if(mListView.getFooterViewsCount() > 0 && mListView.getAdapter() instanceof HeaderViewListAdapter) {
            mListView.removeFooterView(view);
        }
    }

    public void setOnScrollListener(AbsListView.OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    public void setOnPullListener(PullListView.OnPullListener onPullListener) {
        this.onPullListener = onPullListener;
    }

    private void onReachBottom() {
        if(canLoadMore && !isRefreshing) {
            performLoadingMore();
        }
    }

    public void performLoadingMore() {
        if(null != onPullListener && !isLoading) {
            onPullListener.onLoadMore(mListView);
        }
        if(indexOfChild(mFooterView) == -1) {
            addFooterView(mFooterView);
        }
        mFooterView.onLoading(this);
        isLoading = true;
    }

    public boolean isLoading() {
        return isLoading;
    }

    public void setFooterView(DefaultFooterView view) {
        mFooterView = view;
    }

    public void onLoadComplete(boolean isLast,CharSequence message) {
        if(isLast) {
            canLoadMore = false;
            isLoading = false;
            mFooterView.onLoadComplete(true,message);
        } else {
            removeFooterView(mFooterView);
            isLoading = false;
        }
    }


    public void onLoadingError(String error) {
        mFooterView.onLoadError(this,0,error);
        isLoading = false;
    }

    public void reset() {
        isLoading = false;
        canLoadMore = true;
        removeFooterView(mFooterView);
    }

    @Override
    public void onUIReset(PtrFrameLayout frame) {
        isRefreshing = false;
    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        isRefreshing = true;
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        isRefreshing = false;
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {

    }

    public boolean canLoadMore() {
        return canLoadMore;
    }
}
